package com.woniu.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiniu.util.Auth;
import com.woniu.domain.*;
import com.woniu.service.CourseSectionService;
import com.woniu.service.CourseService;
import com.woniu.service.CoursewareService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;



import javax.servlet.http.HttpServletRequest;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


/**
 * Auther: zcl  <br/>
 * Date: 2021/6/7:19:03  <br/>
 * Description:
 */

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

import java.util.List;


@RestController
@RequestMapping("/courseware")
public class CoursewareController {
    @Autowired(required = false)
    CoursewareService coursewareService;

    @Autowired(required = false)
    CourseService courseService;

    @Autowired(required = false)
    CourseSectionService courseSectionService;

    @Autowired(required = false)
    RedisTemplate redisTemplate;

    /**
     * 课件分页列表
     * @param startpage
     * @param pagesize
     * @return
     */
@ApiOperation(value = "分页查询课件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="startpage",value="当前页码",required=true,  dataType="Integer" , dataTypeClass = Integer . class ),
            @ApiImplicitParam(name="pagesize",value="每行页数",required=true,  dataType="Integer" , dataTypeClass = Integer . class )
    })
    @GetMapping("/warePage")
    public PageInfo<Courseware> findWareByPage(Integer startpage, Integer pagesize){
        PageInfo<Courseware> coursewarePageInfo=coursewareService.findCoursewareByPage(startpage, pagesize);
        return coursewarePageInfo;
    }

    /**
     * 根据id删除课件信息
     * @param coursewareId
     * @return
     */
    @ApiOperation(value = "根据课件id删除课件")
    @ApiImplicitParam(name="id",value="课件id",required=true, dataType="Integer" , dataTypeClass = Integer . class )
    @GetMapping("/delWareById")
    public String delWareById(Integer coursewareId){
        coursewareService.delWareById(coursewareId);
        return "success";
    }

    /**
     *  条件搜索
     * @param
     * @return
     */
    @ApiOperation(value = "根据条件搜索课件并分页")
    @ApiImplicitParam(name="courseManagementDTO",value="搜索条件对象",required=true, dataType="CourseManagementDTO<Courseware>")
    @PostMapping("/getWareByCondition")
    public PageInfo<Courseware> getWareByCondition(@RequestBody CourseManagementDTO<Courseware> courseManagementDTO){
        PageHelper.startPage(courseManagementDTO.getPage(),courseManagementDTO.getPageSize());
        List<Courseware> coursewares=coursewareService.getCourseByCondition(courseManagementDTO.getObj());
        return new PageInfo<>(coursewares);
    }

    /**
     * 课件分类
     * @return
     */
    @ApiOperation(value = "查询课件分类列表")
    @PostMapping("/wareCarion")
    public List<WareCation> menuQuery(){
        return coursewareService.wareQuery();
    }

    /**
     * 课件详情
     */
    @ApiOperation(value = "根据课件id查询课件详情")//方法说明
    /* @ApiImplicitParams参数说明
    //显示当前用户的所有考试列表
    name :参数名
    value :参数的说明、描述
    required :参数是否必须必填
    paramType : 参数放在哪个地方
        query --> 请求参数的获取: @RequestParam
        header --> 请求参数的获取: @RequestHeader
        path (用于restful接口) -->请求参数的获取: @PathVariable
        body (请求体) --> @RequestBody User user
        form (普通表单提交)
    dataType : 参数类型，默iString， 其它值dataType= "Integer"
    dataTypeClass :必须加
     */
    @ApiImplicitParam(name = "coursewareId", value = "课件id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    @GetMapping("/getCourseById")
    public Courseware getCourseById(Integer coursewareId) {
        System.out.println(coursewareId);
        System.out.println(coursewareService.getCourseById(coursewareId));
        return coursewareService.getCourseById(coursewareId);

    }

    /**
     * 添加课件
     */
    @ApiOperation(value = "添加课件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseware", value = "课件对象", required = true, paramType = "body", dataType = "Courseware", dataTypeClass = Courseware.class),
            @ApiImplicitParam(name = "httpServletRequest", value = "用户获取请求头信息（登录token）")
    })
    @PostMapping("/addCourseware")
    public int addCourseware(@RequestBody Courseware courseware, HttpServletRequest httpServletRequest) {
        //从redis中通过token键值取出对应的对象
        User user = (User) redisTemplate.opsForValue().get(httpServletRequest.getHeader("Authorization"));
        courseware.setUserId(user.getId());
        System.out.println(courseware);
        int res = coursewareService.addCourseware(courseware);
        return res;
    }

    /**
     * 修改课件
     */
    @ApiOperation(value = "根据课件id修改课件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseware", value = "课件对象", required = true, paramType = "body", dataType = "Courseware", dataTypeClass = Courseware.class),
            @ApiImplicitParam(name = "id", value = "课件id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),

    })
    @PostMapping("/updateCourseware")
    public int updateCourseware(@RequestBody Courseware courseware, Integer id) {
        System.out.println(id);
        System.out.println(courseware);
        courseware.setId(id);
        int res = coursewareService.updateCourseware(courseware);
        return res;
    }

    /**
     * 获得云端token
     *
     * @return
     */
    @ApiOperation(value = "将课件视频及图片上传到云端时需要获取云端的token")
    @PostMapping("/getUpToken")
    public String getUpToken() {
        //最简单的上传凭证只需要AccessKey，SecretKey和Bucket就可以
        String accessKey = "Cx6qcSumdh6eFQGexCl0KQePStNj42kekeJaSMbS";
        String secretKey = "9F4gi3egANQX-E6qZdqLtWrna8LcrDyc0I7JfDM-";
        String bucket = "zengchenlin";
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);// 把这个upToken传递给前端
        return upToken;
    }

    //获取课程下拉框
    @ApiOperation(value = "添加课件时，初始化课程下拉框")
    @GetMapping("/getcourseList")
    public List<Course> courseQuery() {
        List<Course> course = courseService.courseQuery();
        return course;
    }


    @ApiOperation(value = "根据课程id显示对应章节")
    @ApiImplicitParam(name = "courseId", value = "课程id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    @GetMapping("/getcourseSectionByCid")
    public List<CourseSection> getCourseSectionByCid(Integer courseId) {
        List<CourseSection> courseSectionList = courseSectionService.getCourseSectionByCid(courseId);
        return courseSectionList;
    }


    ////查询所有没有对应章节的课件
    @RequestMapping("/coursewareQuery")
    public List<Courseware> coursewareQuery(){
       return  coursewareService.coursewareQuery();
    }
	
	
    @ApiOperation(value = "根据课程id查询对应课件")
    @GetMapping("/getDate")
    public List<Courseware> getDate(Integer courseId){
        System.out.println("进来了！！！");

        return coursewareService.getDate(courseId);
    }
}
